//
//  WLLStatusCell.swift
//  WLLWeibo
//
//  Created by 王乐乐 on 16/8/13.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit
class WLLStatusCell: UITableViewCell {
    
     // 底部视图的顶部约束
    var bottomViewTopConstraint:Constraint?
    
    
    //MARK: - 创建一个 ViewModel 供外界赋值
    var statusViewModel :WLLStatusViewModel?{
        didSet{
            //原创 weibo
            originalView.statusViewModel = statusViewModel
             //MARK: -         // 转发微博
    
            /*
             - 转发微博逻辑分析
             - 如果retweeted_status == nil  没有转发微博
             - 底部视图的top == 原创微博的bottom
             
             - 反之 有转发微博
             - 赋值
             - 底部视图的top == 转发微博的bottom
             */
            //卸载约束
            bottomViewTopConstraint?.uninstall()
            //判断是否有转发微博
            if statusViewModel?.statusModel?.retweeted_status == nil {//没有转发微博
                bottomView.snp_updateConstraints(closure: { (make) in
                  bottomViewTopConstraint =  make.top.equalTo(originalView.snp_bottom).constraint
                })
                //隐藏转发微博( cell的复用)
                retweetView.hidden = true
            }else{//有转发微博
                //先赋值
                retweetView.statusViewModel = statusViewModel
                //设置约束
                bottomView.snp_updateConstraints(closure: { (make) in
                bottomViewTopConstraint  =  make.top.equalTo(retweetView.snp_bottom).constraint
                })
                
                //显示转发微博
                 retweetView.hidden = false
            }
                //底部视图赋值
            bottomView.statusViewModel = statusViewModel
            
        }
    }
    
    
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
//        printLog(self.bounds)
//        printLog(self.contentView)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 设置视图
    private  func setupUI()  {
        //cell 的背景色
        //        backgroundColor = RandomColor()
//        separatorInset = UIEdgeInsetsZero
     
        backgroundColor = StatusCellColor
        //添加控件
        //    print(contentView.frame)
        //    printLog(contentView)
        //    printLog(self.frame)

        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(bottomView)
        
        //添加约束
        //01原创微博
        originalView.snp_makeConstraints { (make) in
            make.top.equalTo(8)
            make.leading.trailing.equalTo(contentView)
            //在自己的类中约束
            //            make.height.equalTo(50)
        }
        //02转发微博
        retweetView.snp_makeConstraints { (make) in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.trailing.equalTo(contentView)
            
        }
        //03底部视图
        bottomView.snp_makeConstraints { (make) in
           bottomViewTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
            make.height.equalTo(35)
            make.leading.trailing.equalTo(contentView)
        }
              contentView.snp_makeConstraints { (make) in
            make.leading.top.trailing.equalTo(self)
            make.bottom.equalTo(bottomView)
        }
        
    }
    
    
    //MARK: - 懒加载控件
    /// 原创微博
    private lazy var originalView:WLLStatusOriginalView  = WLLStatusOriginalView()
    ///转发微博
    private lazy var retweetView:WLLStatusRetweetView = WLLStatusRetweetView()
    ///底部视图
    private lazy var bottomView:WLLStatusBottomView = WLLStatusBottomView()
}






